/**
 * Created by rongyao on 2017/9/19.
 */
requirejs.config({
	baseUrl: "../js/lib",
	paths: {
		"jquery": "jquery-3.2.1",
		"swiper":"swiper.jquery",
		"cookie":"jquery.cookie"
	}
})

requirejs(['jquery',"cookie"], function ($) {
	//加载用户，如果直接打开页面，获取cookie中的用户
	//如果cookie中没有数据 就不加载

	
	
	//获取当前用户
	var strT = window.location.href;
	if(strT.indexOf("?")!=-1){
		var strArr = strT.split("?");
		if(strArr[1].indexOf("=")){
			var newArr = strArr[1].split("=");
		}
	}
	
	
	if(newArr){
		if(newArr[0]=="username"){
			window.username = newArr[1];
		}
	}
	
	if(!window.username){
		
		var str = $.cookie("users");
		if (str) {
			window.username = JSON.parse(str)[0].username;
		}
	}
	console.log(window.username);
	
	//鼠标悬停，显示购物车列表
	$(".nav_right").on("mouseenter",function () {
		$(".hid_goods").css("height",$(".goods_list").height());
	});
	//鼠标离开，消失
	$(".nav_right").on("mouseleave",function () {
		$(".hid_goods").css("height",0);
	});
	
	//动态添加下拉列表
	$.ajax({
		url: "../json/second.json",
		type: "get",
		async: true,
		success: function (res) {
			$(".nav_left>li:not([class*='hh'])").each(function (index,ele) {
				//动态创建li加a
				for (var i = 0; i < res[index].hotActivities.length; i++) {
					var $li = $("<li></li>");
					$li.html("<a href=''>"+res[index].hotActivities[i]+"</a>");
					$(ele).find("ul").eq(0).append($li);
				}
				for (var j = 0; j < res[index].goods.length; j++) {
					var $li = $("<li></li>");
					$li.html("<a href=''>"+res[index].goods[j]+"</a>");
					$(ele).find("ul").eq(1).append($li);
				}
				//鼠标移入 显示二级列表
				$(ele).on("mouseenter",function () {
					$(ele).find(".pop_up").css("display","block");
				})
				//鼠标移出二级列表消失
				$(ele).on("mouseleave",function () {
					$(ele).find(".pop_up").css("display","none");
				})
			})
			
		}
	})
	
	//动态添加图片和文字
	$.ajax({
		url:"../json/imgsrc.json",
		type: "get",
		async: true,
		success:function (res) {
			var count=0;
			for(var key in res){
				for (var j = 0; j < res[key].length; j++) {
					var $li = $("<li class='list'><a href='goods.html?id="+j+"&key="+key+"&username="+window.username+"'></a></li>");
					$(".devide").eq(count).append($li);
				}
				count++;
				
			}
			// 在今日上新添加内容
			$("#first .list").each(function (index, ele) {
					creat("first",index,ele);
			});
			// 在热门品牌添加内容
			$("#second .list").each(function (index, ele) {
				creat("seconde",index,ele);
			});
			// 在女士活动添加内容
			$("#third .list").each(function (index, ele) {
				creat("third",index,ele);
			});
			//动态创建方法
			function creat(key,index,ele) {
				var $div = $("<div class='scal'></div>");
				var $p = $("<p class='introduce'></p>");
				var $shadow = $("<div class='shadow'></div>")
				$div.html("<div class='hidd'></div>");
				$(ele).children("a").append($div).append($p);
				$(ele).children("a").find(".scal").append($shadow);
				$(ele).children("a").find(".scal").append($("<img/>").attr("src",res[key][index].src));
				$(ele).children("a").find(".hidd").html(res[key][index].hid);
				$(ele).children("a").find("p").html(res[key][index].word+"<span></span>");
				$(ele).children("a").find("span").html("<i>"+res[key][index].sale+"</i>"+"折起");
			}
		}
	})
	
	
	//点击商品跳转到商品详情页 数据传输
	
	
	
	//图片放大
	$(".devide").on("mouseenter",".scal",function () {
		$(this).find("img").css("transform","scale(1.2)");
		$(this).find(".hidd").css("display","block");
		$(this).find(".shadow").css("display","block");
	})
	
	$(".devide").on("mouseleave",".scal",function () {
		$(this).find("img").css("transform","scale(1)");
		$(this).find(".hidd").css("display","none");
		$(this).find(".shadow").css("display","none");
	})
	
	
	
	
	
	$.ajax({
		url:"../json/activity.json",
		type: "get",
		async: true,
		success:function (res) {
			$(".tab_top li").each(function (index,ele) {
				var $div = $("<div class='coall clear'></div>");
				$(".tab_main").append($div);
				for (var i = 0; i < res[index].length; i++) {
					$div.html($div.html()+"<div class='theAct'><div class='bd'></div><div class='button'></div><div class='img'><img src="+res[index][i].img+" alt=''></div><div class='message'><div>"+res[index][i].des+"</div><span><i>"+res[index][i].scal+"</i>折起</span></div>");
				}
				
			})
			$(".button").html("<a class='a3'>预览</a><span>|</span><a class='a3'>订阅</a>");
			$(".tab_top li").eq(0).css("borderBottom","3px solid black");
			$(".tab_main .coall").eq(0).css("display","block");
			$(".tab_top li").on("click",function () {
				var index = $(this).index();
				$(".tab_top li").css("borderBottom","0")
				$(this).css("borderBottom","3px solid black");
				$(".tab_main .coall").css("display","none");
				$(".tab_main .coall").eq(index).css("display","block");
			})
		}
	})
	
	//给theAct添加鼠标悬停事件
	$("body").on("mouseenter",".theAct",function () {
		//清空
		$(this).find(".bd").css("background","");
		$(".button").css("display","none");
		//将鼠标移过的元素添加样式
		$(this).find(".bd").css("background","rgba(0,0,0,0.8)");
		$(this).find(".button").css("display","block");
	})
	//鼠标离开
	$("body").on("mouseleave",".theAct",function () {
		$(this).find(".bd").css("background","");
		$(this).find(".button").css("display","none");
	})
	
	//给button div中添加内容
	
	
	//轮播图 使用swiper插件
	requirejs(['swiper'],function () {
		var mySwiper = new Swiper('.swiper-container', {
			autoplay: 5000,//可选选项，自动滑动
			autoplayDisableOnInteraction : false,autoplayDisableOnInteraction : false,
			pagination : '.swiper-pagination',
			paginationClickable :true,
			prevButton:'.swiper-button-prev',
			nextButton:'.swiper-button-next',
			loop: true,
			speed:1000
		})
	})
	
	// 控制主体部分随滚动条滚动改变margin-top值
	
	//将导航条的高度记下 方便后面判断nav是否要固定顶部
	var topY = $("#nav").offset().top;
	//记下main的marginTop值 滚轮往下滚 margin-top值减小 知道0
	var marginT = $("#main").css("marginTop");
	
	var nowMargin;
	$(window).on("scroll",function () {
		//滚轮往下滚 margin-top值减小
		nowMargin = parseInt(marginT)-$(document).scrollTop()*1.5;
		if(nowMargin<=0){
			nowMargin=0;
		}
		$("#main").css("marginTop",nowMargin);
		
		//导航条固定顶部判断
		if($(document).scrollTop()>=topY){
			//scrollTop值大于nav的offsetTop 将nav固定在顶部
			$("#logo").css("marginBottom","42px");//原先位置用margin代替
			$("#nav").css({
				"position":"fixed",
				"top":0,
				"width":"100%"
			});
		}else{
			//否则回到原处
			$("#logo").css("marginBottom","0px");
			$("#nav").css({
				"position":"static"
			});
		}
		
		//侧边放回顶部按钮显示和消失
		if(parseInt($(document).scrollTop())>=300){
			$(".clickOn").css("display","block");
		}else{
			$(".clickOn").css("display","none");
		}
		//返回顶部
		$(".clickOn").on("click",function () {
			scrollTo(0,0);
		})
	})
	
	//init 购物车
	if(!window.username){
		//如果当前账户没有登录 点击跳转到登陆界面
		$(".nav_right").on("click",function () {
			window.location.href="login.html";
		})
	}else{
		//如果当前有账户登录 点击跳转到购物车页面
		$(".nav_right").on("click",function () {
			window.location.href="shopCar.html?username="+window.username+"";
		})
		$(".topLeft").html("<li></li><li class='user'><a href=''>"+username+"</a></li><li class='tui'><a>退出</a></li><li>|</li><li><a href=''>收藏</a></li>");
		//点击退出
		$(".tui").on("click",function () {
			requirejs(['cookie'],function () {
				$.removeCookie("users");
			})
			window.location.href = "index.html";
		})
		$.ajax({
			url:"http://localhost/hz1706/mei/init.php",
			dataType:"json",
			type:"post",
			data:{
				username:username
			},
			success:function (res) {
				console.log(res);
				$(".goods_list").html('');
				if(res.data[0].shopGoods){
					var arr = JSON.parse(res.data[0].shopGoods);
					for (var i = 0; i < arr.length; i++) {
						$(".goods_list").append("<dl class='clear'><dt><img src="+arr[i].src+" alt=''></dt><dd><p><a href=''>"+arr[i].goodsname+"</a><span>"+arr[i].count+"*"+arr[i].price+"</span></p><a class='remove'>删除</a></dd></dl>");
					}
				}
				
				//右边购物车里边显示商品数量和总价格
				$(".shopping_quantity span").eq(0).text($("dl").length);
				var sum = 0;
					$("dl").each(function (index, ele) {
					var str = $(ele).find("span").text();
					var arr = str.split("*");
					var price=arr[1];
					if(arr[1].indexOf(",")!=-1){
						var arr1 = arr[1].split(",");
						price = arr1[0]+arr1[1];
					}
					sum+=Number(arr[0])*Number(price);
				})
				$(".shopping_amount").text("¥"+sum+".00");
			}
		})
	}
})



